﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChatData
{
    public interface IGetTime
    {
        DateTime getTime();
    }
    /// <summary>
    /// Stores Controls in DateTimeOrder
    /// </summary>
    public class ControlSortedList : SortedList<DateTime,Control> 
    {

    }
    /// <summary>
    /// Stores items for retirval in datetime order. Items must implement IGetTime
    /// </summary>
    public class TimeStream<T> : List<T> where T : IGetTime
    {
        //from http://stackoverflow.com/questions/396621/linq-combine-multiple-listt-and-order-by-a-value-net-35
        public TimeStream() { }
        public TimeStream(List<T> thedata)
        {
            foreach (T item in thedata)
            {
                this.Add(item);
            }
        }
        public static List<T> Combine(TimeStream<T> one, TimeStream<T> two, TimeStream<T> three)
        {
            return one.Concat(two)
          .Concat(three)
          .OrderBy(x => x.getTime())
          .ToList();
        }
        public TimeStream<T> getStreamSince(DateTime time)
        {
            var thestream = from T item in this
                            where item.getTime()> time
                            select item;
           return new TimeStream<T>(thestream.ToList<T>());
        }
        public TimeStream<T> OrderByDateAsc()
        {
            var thestream = from T item in this
                            orderby item.getTime() ascending
                            select item;
            return new TimeStream<T>(thestream.ToList<T>());
        }
        public TimeStream<T>OrderByDateDsc()
        {
            var thestream = from T item in this
                            orderby item.getTime() descending
                            select item;
            return new TimeStream<T>(thestream.ToList<T>());
        }
    }
}
